package com.qf.service.Impl;

import com.qf.mapper.DeviceMapper;
import com.qf.model.Device;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;
    @Override
    public List<Device> findAll() {
        List<Device> list = deviceMapper.getAll();
        return list;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
//         total总记录数
//         rows当前页结果
        PageResult pageResult =new PageResult();
        Integer currentPage = queryPageBean.getCurrentPage();//当前页
        Integer pageSize = queryPageBean.getPageSize();//每页大小
        List<Device> page = deviceMapper.findPage((currentPage-1)*pageSize,pageSize);
        long count = deviceMapper.count();
        pageResult.setRows(page);
        pageResult.setTotal(count);
        return pageResult;
    }
    @Override
    @Transactional
    public int addDevice(Device device) {
        device.setUpdatetime(new Date());
        int row = deviceMapper.insertDevice(device);
        return row;
    }

    @Override
    public Device findById(int id) {
        Device device = deviceMapper.findDevice(id);
        return device;
    }


    @Override
    @Transactional
    public int deleteById(Integer id) {
        int row = deviceMapper.delDevice(id);
        return row;
    }

    @Override
    @Transactional
    public int edit(Device device) {
        device.setCreatetime(new Date());
        device.setUpdatetime(new Date());
        int row = deviceMapper.update(device);
        return row;
    }


}
